package cn.zy.company.service;

import cn.zy.company.dao.UserDao;
import cn.zy.company.entity.PageObj;
import cn.zy.company.entity.User;
import cn.zy.company.entity.Work;

import java.sql.SQLException;
import java.util.List;

public class UserService {
    UserDao userDao = new UserDao();

    public void add(User user) throws Exception {
        userDao.add(user);
    }

    public void delete(Integer id) throws Exception {
        userDao.delete(id);
    }

    public void update(User user) throws Exception {
        userDao.update(user);
    }

    public User findById(Integer id) throws Exception {
        return userDao.findById(id);
    }

    public User find(String username, String password) throws Exception {
        return userDao.find(username, password);

    }
    public PageObj<User> find(String username, PageObj<User> page) throws Exception {
        List<User> all = userDao.find(username,page.getPage(), page.getSize());
        page.setList(all);
        long l = userDao.countAll();
        page.setCount(l);
        Integer size = page.getSize();
        long l1 = l % size != 0 ? (l / size + 1) : (l / size);
        page.setPageCount((int) l1);
        return page;

    }
    public List<User> findAll() throws Exception {
        return userDao.findAll();
    }
    public PageObj<User> findAll(PageObj<User> page) throws SQLException {
        List<User> all = userDao.findAll(page.getPage(), page.getSize());
        page.setList(all);
        long l = userDao.countAll();
        page.setCount(l);
        Integer size = page.getSize();
        long l1 = l % size != 0 ? (l / size + 1) : (l / size);
        page.setPageCount((int) l1);
        return page;
    }
}
